'use strict';
import {ajax, nameSpace } from 'utils/index';

let ns = nameSpace('Zhekou1');
export const SET_FIELD = ns('SET_FIELD');
export const GET_ITEMS_ON_SEAL = ns('GET_ITEMS_ON_SEAL');
export const GET_ITEMS_IN_DEPOT = ns('GET_ITEMS_IN_DEPOT');
export const GET_SELLER_CATS = ns('GET_SELLER_CATS');
export const SHOW_LOADING = ns('SHOW_LOADING');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const LOAD_ERR = ns('LOAD_ERR');
export const SET_ITEMS = ns('SET_ITEMS');
export const ADD_ITEM_TO_ACTIVE = ns('ADD_ITEM_TO_ACTIVE');
export const LOAD_SET_ITEMS_IN_SESSION = ns('LOAD_SET_ITEMS_IN_SESSION');
export function setField(name,value) {
  return {type: SET_FIELD,name: name,value:value};
}
export function closeDialog(name,value) {
  return {type: CLOSE_DIALOG,visible:false};
}
export function errDialog(msg) {
  return {type: LOAD_ERR,...msg};
}
export function setItems(itemset) {
  return {type: SET_ITEMS,itemset};
}
export function loadSetItemsInSession(action) {
	var itemset = [];
	if(action.set_items){
		let items = JSON.parse(action.set_items);
		if(items instanceof Array){
			items.map(item=>{
				itemset[item.num_iid]=item;
			});
		}
	}
  return {type: LOAD_SET_ITEMS_IN_SESSION,itemset};
}
export function getSellerCats(params) {
		return (dispatch) => {
			//发送请求前
			dispatch({
			  type: SHOW_LOADING,
			  loading:true
			});
			if(!params.nick){
				dispatch({
					type: LOAD_ERR,
					message:"登陆信息丢失，请重新登陆",
					loginFalse:true
				});
				return;
			}
			if(window.workbench){
				//千牛环境执行创建折扣,含有workbench参数为千牛环境
				dispatch({
						type: LOAD_ERR,
						message:"抱歉，当前版本不支持千牛环境。请联系旺旺：kekydream 获取千牛版本。",
						loginFalse:false
					});
				return;
				  window.QN.top.invoke( {
						cmd : 'taobao.sellercats.list.get',
						param : params,
						method:"post",
						success : function(rsp, cmd, param) {
							console.log(rsp,param);
						},
						error : function(msg, cmd, param) {
							console.log(msg,param);
						}
					} );
			}else{
				ajax({
				  api: 'getSellerCats',
				  method: 'GET',
				  data:params
				},(json) => {
					if(json.seller_cats && !(json.seller_cats.seller_cat instanceof Array)){
						json.seller_cats.seller_cat = Array(json.seller_cats.seller_cat);
					}
					dispatch({
					  type: GET_SELLER_CATS,
					  seller_cats:json.seller_cats.seller_cat
					});
				},(json) => {
					const login = (json.code==210);
					dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
					})
				});
			}
		}
}
export function addItemToActive(itemset) {
	let params = {
		activity_id : itemset.activity_id,
		item_id:itemset.num_iid,
		promotion_value:itemset.promotion_value*100,
		promotion_type:itemset.promotion_type
	}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.detail.add',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						let setItems = sessionStorage.getItem("setItems");
						if(setItems){
							setItems = JSON.parse(setItems);
						}else{
							setItems = Array();
						}
						setItems.push({...itemset,status:"done",detail_id:json.detail_id});
						sessionStorage.setItem("setItems",JSON.stringify(setItems));
						dispatch({type: ADD_ITEM_TO_ACTIVE,itemset});
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:msg,
							loginFalse:false
						});
					}
				} );
		}else{
			ajax({
			  api: 'addItemsToActive',
			  method: 'GET',
			  data:params
			},(json) => {
				let setItems = sessionStorage.getItem("setItems");
				if(setItems){
					setItems = JSON.parse(setItems);
				}else{
					setItems = Array();
				}
				const newItemSet = {...itemset,status:"done",detail_id:json.detail_id};
				setItems.push(newItemSet);
				sessionStorage.setItem("setItems",JSON.stringify(setItems));
				dispatch({type: ADD_ITEM_TO_ACTIVE,newItemSet});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function editItemInActive(itemset) {
	let params = {
		activity_id : itemset.activity_id,
		item_id:itemset.num_iid,
		promotion_value:itemset.promotion_value*100,
		promotion_type:itemset.promotion_type,
		detail_id:itemset.detail_id
	}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.detail.update',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						let setItems = sessionStorage.getItem("setItems");
							setItems = JSON.parse(setItems);
						sessionStorage.setItem("setItems",JSON.stringify(setItems));
						dispatch({type: ADD_ITEM_TO_ACTIVE,itemset});
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:msg,
							loginFalse:false
						});
					}
				} );
		}else{
			ajax({
			  api: 'editItemInActive',
			  method: 'GET',
			  data:params
			},(json) => {
				let setItems = sessionStorage.getItem("setItems");
					setItems = JSON.parse(setItems);
					if(setItems instanceof Array){
						setItems.map((item,key)=>{
							if(item.num_iid==params.item_id){
								setItems[key].promotion_type = itemset.promotion_type;
								setItems[key].promotion_value = itemset.promotion_value;
								setItems[key].ztype = itemset.promotion_type==0?"decreaseAmount":"discountRate";
								setItems[key].value = itemset.promotion_value;
								switch(itemset.promotion_type){
									case 0 :
										setItems[key].finaPrice = itemset.price - itemset.promotion_value;
									break;
									case 1 :
										setItems[key].finaPrice = itemset.price * (itemset.promotion_value/10);
									break;
								}
							}
						});
					}
				sessionStorage.setItem("setItems",JSON.stringify(setItems));
				dispatch({
				  type: SHOW_LOADING,
				  loading:false
				});
				dispatch({type: ADD_ITEM_TO_ACTIVE,itemset});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsIndepot(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.inventory.get',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsIndepot',
			  method: 'GET',
			  data:params
			},(json) => {
				if(json.total_results>0){
					if(!(json.items.item instanceof Array)){
						json.items.item = Array(json.items.item);
					}
				}else{
					json.items={item:[]};
				}
				
				dispatch({
				  type: GET_ITEMS_IN_DEPOT,
				  items:json.items.item,
				  total_results:json.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsOnSale(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.onsale.get',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsOnSale',
			  method: 'GET',
			  data:params
			},(json) => {
				if(json.total_results>0){
					if(!(json.items.item instanceof Array)){
						json.items.item = Array(json.items.item);
					}
				}else{
					json.items={item:[]};
				}
				
				dispatch({
				  type: GET_ITEMS_ON_SEAL,
				  items:json.items.item,
				  total_results:json.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
export function getItemsByIds(params) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: SHOW_LOADING,
		  loading:true
		});
		if(window.workbench){
			//千牛环境执行创建折扣,含有workbench参数为千牛环境
			  window.QN.top.invoke( {
					cmd : 'taobao.items.seller.list.get',
					param : {ids:params.ids},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			ajax({
			  api: 'getItemsByIds',
			  method: 'GET',
			  data:{ids:params.ids}
			},(json) => {
				if(json.items.item && !(json.items.item instanceof Array)){
					json.items.item = Array(json.items.item);
				}
				dispatch({
				  type: GET_ITEMS_ON_SEAL,
				  items:json.items.item,
				  total_results:params.total_results,
				  page:params.page,
				  pagesize:params.pagesize,
				  cids:params.cids,
				  q:params.q
				});
			},(json) =>{
				const login = (json.code==210);
				dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				});
			});
			
		}
	}
}
